package com.apes.fn.tms.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.tms.model.BranchLine;
import com.apes.fn.tms.model.Chauffeur;
import com.apes.fn.tms.repository.BranchLineRepository;
import com.apes.fn.tms.repository.ChauffeurRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.EventUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.recruit.model.ApplyJobOrder;
import com.apestech.framework.json.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("chauffeurService")
public class ChauffeurService extends DomainService {
    @Autowired
    ChauffeurRepository chauffeurRepository;

    public Chauffeur create(SimpleRequest simpleRequest){
        Chauffeur chauffeur = simpleRequest.getO(Chauffeur.class);
        return chauffeurRepository.saveAndFlush(chauffeur);
    }


    public Chauffeur createByApplyJobOrder(SimpleRequest request){
        Employee employee = request.getO(Employee.class);
        Chauffeur chauffeur = chauffeurRepository.findOne(employee.getId());
        if (employee.getPost().getName().equals("物流司机")){
            if (chauffeur==null) chauffeur = new Chauffeur();
            chauffeur.setId(employee.getId());
            chauffeur.setName(employee.getName());
            chauffeur.setEmployeeState(employee.getEmployeeState());
            chauffeur.setMobileNumber(employee.getMobileNumber());
            chauffeur.setSex(employee.getSex());
            chauffeur.setWorkPost(employee.getPost());
            chauffeur.setParty(employee.getParty());
            chauffeur = chauffeurRepository.saveAndFlush(chauffeur);
        }
        return chauffeur;
    }

    public Chauffeur update(SimpleRequest simpleRequest){
        Chauffeur chauffeur = simpleRequest.getO(Chauffeur.class);
        return chauffeurRepository.saveAndFlush(chauffeur);
    }

    public Chauffeur updateState(SimpleRequest simpleRequest){
        Employee employee = simpleRequest.getO(Employee.class);
        Chauffeur chauffeur = chauffeurRepository.findOne(employee.getId());
        if (employee.getPost().getName().equals("物流司机")){
            chauffeur.setEmployeeState(employee.getEmployeeState());
            chauffeur = chauffeurRepository.saveAndFlush(chauffeur);
        }
        return chauffeur;
    }

    private String delete(SimpleRequest simpleRequest){
        Integer versions = simpleRequest.get("version");
        Chauffeur chauffeur = simpleRequest.getO(Chauffeur.class);
        if (chauffeur.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        chauffeurRepository.delete(chauffeur);
        return "删除成功";
    }


    /**
     * 验证手机号
     * 验证司机
     * @param request
     * @return
     */
    public Chauffeur checkChauffeur(SimpleRequest request){
        Map m = request.getMap();
        String phone = (String) m.get("phone");
        Chauffeur chauffeur = chauffeurRepository.findByMobileNumber(phone);
        if (chauffeur==null) throw new RuntimeException("此手机号不是“集群车宝员工--司机”手机号，请核实！");
        return chauffeur;
    }

    /**
     * 登录TMS司机移动端
     * 验证司机
     * @param request
     * @return
     */
    public Chauffeur loginTMS(SimpleRequest request){
        Map m = request.getMap();
        String phone = (String) m.get("phone");
        Chauffeur chauffeur = chauffeurRepository.findByMobileNumber(phone);
        if (chauffeur==null) throw new RuntimeException("此手机号不是“集群车宝员工--司机”手机号，请核实！");
        // 调用App短信验证平台
//        checkCaptchaApp(request);
        return chauffeur;
    }


    /**
     * 查询可以指派任务的司机
     * @param request
     * @return
     */
    public Page findAllByAssigned(SimpleRequest request){
        return chauffeurRepository.findAllByAssigned(PageRequest.of(request.get("page"), request.get("size")));
    }

    public Page<Chauffeur> findAll(SimpleRequest request) {
        try {
            JSONObject object = request.get("group");
            List<Map> filters =(List<Map>) object.get("condition");
            Sort sort = new Sort(Sort.Direction.DESC, "id");
            return chauffeurRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"),sort));
        } catch (Exception e){
            List<Map> filters = request.get("condition");
            Sort sort = new Sort(Sort.Direction.DESC, "id");
            return chauffeurRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"),sort));
        }
    }

}
